package com.weetop.cfw.message.fragment

import com.blankj.utilcode.util.ActivityUtils
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.weetop.cfw.R
import com.weetop.cfw.base.presenter.imp.MessagePresenterImp
import com.weetop.cfw.base.ui.fragment.CommonBaseFragment
import com.weetop.cfw.base.view.MessageView
import com.weetop.cfw.bean.MessageListBean
import com.weetop.cfw.bean.TestBean
import com.weetop.cfw.message.activity.MessageDetailActivity
import com.weetop.cfw.message.adapter.SystemMessageAdapter
import com.weetop.cfw.utils.MMKVUtils
import kotlinx.android.synthetic.main.fragment_consumption_message.*


/**
 * 消费消息界面
 */
class ConsumptionMessageFragment: CommonBaseFragment(),MessageView,OnRefreshLoadMoreListener {

    private lateinit var systemMessageAdapter:SystemMessageAdapter

    private lateinit var messagePresenterImp: MessagePresenterImp

    private var currentPageNumber:Int = 1//当前数据的页数
    private var totalCount:Int = 0//数据的总记录数
    private var isRefreshing:Boolean = false//当前是否处于刷新状态
    private var isLoadingMore:Boolean = false//当前是否处于加载更多状态

    private val messageList = ArrayList<MessageListBean.DataBean>()

    override fun getLayoutId() = R.layout.fragment_consumption_message

    override fun initView() {

    }

    override fun initData() {

        messagePresenterImp = MessagePresenterImp()
        messagePresenterImp.attachView(this)

        consumptionMessageRefreshLayout.setOnRefreshLoadMoreListener(this)

        systemMessageAdapter = SystemMessageAdapter(R.layout.layout_item_consuption_message,messageList)
        systemMessageAdapter.setOnItemClickListener { _, _, position ->
            MessageDetailActivity.startMessageDetailActivity(context!!,messageList[position].infoid)
        }
        consumptionMessageRV.adapter = systemMessageAdapter
    }

    override fun onDestroyView() {
        super.onDestroyView()
        messagePresenterImp.detachView()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        if(isFirstLoadData){
            if(MMKVUtils.getToken().isNotEmpty()){//如果用户登录了，再进行接口请求
                messagePresenterImp.getMessageList(this,"2",currentPageNumber)
            }
        }
    }

    /**
     * 消息列表获取成功
     */
    override fun messageListGetSuccess(messageListBean: MessageListBean?) {
        if(messageListBean != null){
            isFirstLoadData = false
            totalCount = messageListBean.records
            if(isRefreshing){
                messageList.clear()
                messageList.addAll(messageListBean.data)
                systemMessageAdapter.notifyDataSetChanged()
                consumptionMessageRefreshLayout.finishRefresh(true)
                if(messageListBean.data.size > 0){
                    currentPageNumber = 1
                }
                isRefreshing = false
                return
            }
            if(isLoadingMore){
                messageList.addAll(messageListBean.data)
                systemMessageAdapter.notifyDataSetChanged()
                consumptionMessageRefreshLayout.finishLoadMore(true)
                if(messageListBean.data.size > 0){
                    currentPageNumber += 1
                }
                isLoadingMore = false
                return
            }
            messageList.addAll(messageListBean.data)
            systemMessageAdapter.notifyDataSetChanged()
        }
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        if(messageList.size >= totalCount){
            showNativeShortToast("暂无更多数据")
            consumptionMessageRefreshLayout.finishLoadMoreWithNoMoreData()
        }else{
            isLoadingMore = true
            messagePresenterImp.getMessageList(this,"2",currentPageNumber + 1)
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
       isRefreshing = true
        messagePresenterImp.getMessageList(this,"2",1)
    }
}